package com.hanqi.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hanqi.entity.SysRole;
import com.hanqi.entity.SysRoleMenu;
import com.hanqi.service.SysRoleMenuService;
import com.hanqi.util.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zjk
 * @since 2019-04-27
 */
@Controller
@RequestMapping("/sysRoleMenu")
public class SysRoleMenuController {

    @Autowired
    protected SysRoleMenuService sysRoleMenuService;

    @ResponseBody
    @RequestMapping("/updateRoleMenuPermission")
    public JSONObject updateRoleMenuPermission(String menuIds, String roleId) {
        if (StringUtils.isNotEmpty(menuIds) && StringUtils.isNotEmpty(roleId)) {
            QueryWrapper<SysRoleMenu> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("role_id", roleId);
            boolean r = sysRoleMenuService.remove(queryWrapper);
            if (r) {
                boolean insertResuult = sysRoleMenuService.insertRoleMenu(roleId, menuIds.split(","));
                if (insertResuult) {
                    return R.SUCCESS;
                }
            }
        }
        return R.FAIL;
    }

}
